package com.example.bootadmin.controller;

import com.example.bootadmin.entity.MyUser;
import com.example.bootadmin.reqModel.UserPageModel;
import com.example.bootadmin.service.MyUserService;
import com.example.bootadmin.util.AdminPage;
import com.example.bootadmin.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping(value = "user")
public class MyUserController {

    @Autowired
    MyUserService myUserService;

    /**
     * 查询用户列表
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public R list(){
        return R.ok(myUserService.list());
    }

    /**
     * 添加
     * @param myUser
     * @return
     */
    @RequestMapping(value = "save",method = RequestMethod.POST)
    public R save(@RequestBody MyUser myUser){
      int i=  this.myUserService.save(myUser);
      if (i>0){
          return R.ok("添加成功");
      }else {
          return R.error("添加失败");
      }

    }

    /**
     * 修改
     * @param myUser
     * @return
     */
     @RequestMapping(value = "update",method = RequestMethod.POST)
    public R update(@RequestBody MyUser myUser){
        int i=  this.myUserService.update(myUser);
        if (i>0){
            return R.ok("修改成功");
        }else {
            return R.error("修改失败");
        }

    }



    /**
     * 删除用户
     * @param userId
     * @return
     */

    @RequestMapping(value = "delete",method = RequestMethod.POST)
    public R delete(@RequestParam Long userId){
        int i=  this.myUserService.delete(userId);
        if (i>0){
            return R.ok("删除成功");
        }else {
            return R.error("删除失败");
        }

    }
    @RequestMapping(value = "pageList",method = RequestMethod.POST)
    public R pageList(@RequestBody UserPageModel userPageModel){
        List<MyUser> list=this.myUserService.pageList(userPageModel);
        Integer i=this.myUserService.pageTotal(userPageModel);

        return R.ok(new  AdminPage(i,list));
    }


}
